package com.ist412.pocke;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ist412.pocke.DTO.ProfileDTO;
import com.ist412.pocke.SQL.Profile;

/**
 * Displays all the information about the Profile to the user.
 * <p>
 * Queries the SQLite database for ProfileDTO objects to determine if a profile has been created.
 * If a profile exists, it fills the fields with the data, otherwise it fills the fields with
 * the data contained in a default ProfileDTO. 
 * 
 * @author Rob C
 * @version 1.1
 */
public class ProfileActivity extends Activity implements OnClickListener {
	
	private boolean		_profileExists;
	private ProfileDTO 	_profile;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Remove title & Make fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
	    setContentView(R.layout.activity_profile);
	    
	    // Setup navigation
        ImageView imgProfile 	= (ImageView) findViewById(R.id.navigation_imgProfile);
        ImageView imgCalendar 	= (ImageView) findViewById(R.id.navigation_imgCalendar);
        ImageView imgWorkout	= (ImageView) findViewById(R.id.navigation_imgWorkout);
        ImageView imgExercise	= (ImageView) findViewById(R.id.navigation_imgExercise);
        ImageView imgBack		= (ImageView) findViewById(R.id.profile_imgBack);
        ImageView imgSettings	= (ImageView) findViewById(R.id.profile_imgSettings);
        Button btnEdit			= (Button)	  findViewById(R.id.profile_btnEdit);
        
        imgProfile.setEnabled(false);
        // TODO Set this through R.color
        imgProfile.setImageResource(R.drawable.profile_icon_trans_dark);
        imgProfile.setBackgroundColor(MainActivity._BG_BLUE);
        imgCalendar.setOnClickListener(this);
        imgWorkout.setOnClickListener(this);
        imgExercise.setOnClickListener(this);
        imgBack.setOnClickListener(this);
        imgSettings.setOnClickListener(this);
        btnEdit.setOnClickListener(this);
        
        // Determine if profile exists
        _profileExists = checkProfileExists();
        
        if (_profileExists) {
        	// Get profile object
        	ArrayList<ProfileDTO> profiles = Profile.getProfile(this);
        	_profile = profiles.get(profiles.size() - 1);
        	
        	// Show it
        	displayProfile();
        	
        } else {
        	_profile = new ProfileDTO();
        	
        }
        
	} // end onCreate()
	
	@Override
	public void onResume() {
		super.onResume();
		
		_profileExists = checkProfileExists();
		
		// Check if a profile exists now
		if (_profileExists) {
			ArrayList<ProfileDTO> profiles = Profile.getProfile(this);
			// TODO Get profile by id - save ID in the MainActivity
			_profile = profiles.get(profiles.size() - 1);
			
			displayProfile();
		}
		
	} // end onResume()
	
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
			case R.id.navigation_imgWorkout:
				intent = new Intent(v.getContext(), WorkoutActivity.class);
				startActivity(intent);
				finish();
				break;
			case R.id.navigation_imgCalendar:
				finish();
				break;
			case R.id.navigation_imgExercise:
				intent = new Intent(v.getContext(), ExerciseActivity.class);
				startActivity(intent);
				finish();
				break;
			case R.id.profile_btnEdit:
				intent = new Intent(v.getContext(), ProfileEditActivity.class);
				startActivity(intent);
				break;
			case R.id.profile_imgBack:
				finish();
				break;
			case R.id.profile_imgSettings:
				intent = new Intent(this, SettingsActivity.class);
				startActivity(intent);
				break;
		} // end switch(view v)
		
	} // end onClick()
	
	
	/**
	 * Fills the TextViews in ProfileActivity to reflect the values from the active Profile.
	 * <p>
	 * BMI Formula   BMI = ((weight) / ((height in inches)^2)) * 703
	 */
	private final void displayProfile() {
		// Grab widgets
		TextView txtFName		= (TextView) findViewById(R.id.profile_txtFirstName);
		TextView txtLName 		= (TextView) findViewById(R.id.profile_txtLastName);
		TextView txtAge 		= (TextView) findViewById(R.id.profile_txtAge);
		TextView txtGender		= (TextView) findViewById(R.id.profile_txtGender);
		TextView txtWeight		= (TextView) findViewById(R.id.profile_txtWeight);
		TextView txtFeet 		= (TextView) findViewById(R.id.profile_txtFeet);
		TextView txtInch		= (TextView) findViewById(R.id.profile_txtInch);
		TextView txtBMI			= (TextView) findViewById(R.id.profile_txtBMI);
		TextView txtArm			= (TextView) findViewById(R.id.profile_txtArm);
		TextView txtShoulder	= (TextView) findViewById(R.id.profile_txtShoulder);
		TextView txtChest		= (TextView) findViewById(R.id.profile_txtChest);
		TextView txtWaist		= (TextView) findViewById(R.id.profile_txtWaist);
		TextView txtThigh		= (TextView) findViewById(R.id.profile_txtThigh);
		TextView txtCalf		= (TextView) findViewById(R.id.profile_txtCalf);
		
		// Calculate BMI
		double mass = _profile.getWeight();
		double height = (_profile.getHFeet() * 12) + _profile.getHInch();
		double bmi = 0.00;
		if (mass <= 0 || height <= 0) {
			bmi = 0.00;
		} else {
			try {
				bmi = ((mass) / Math.pow(height, 2)) * 703;
				bmi = Double.parseDouble(new DecimalFormat("#.##").format(bmi));
			} catch (Exception e) { e.printStackTrace(); bmi = 0.00; }
		}
		
		// Fill Fields
		txtFName.setText(_profile.getFName());
		txtLName.setText(_profile.getLName());
		txtAge.setText(Integer.toString(_profile.getAge()));
		txtGender.setText(_profile.getGender());
		txtWeight.setText(Integer.toString(_profile.getWeight()));
		txtFeet.setText(Integer.toString(_profile.getHFeet()));
		txtInch.setText(Integer.toString(_profile.getHInch()));
		if (bmi > 0.00) {
			txtBMI.setText(Double.toString(bmi));
		} else {
			txtBMI.setText("Unable to calculate BMI");
		}
		txtArm.setText(Integer.toString(_profile.getBodyArm()));
		txtShoulder.setText(Integer.toString(_profile.getBodyShoulder()));
		txtChest.setText(Integer.toString(_profile.getBodyChest()));
		txtWaist.setText(Integer.toString(_profile.getBodyWaist()));
		txtThigh.setText(Integer.toString(_profile.getBodyThigh()));
		txtCalf.setText(Integer.toString(_profile.getBodyCalf()));
		
	} // end displayProfile()
	
	/**
	 * This function checks whether or not a profile exists within the SQLite database.
	 * @return
	 */
	private final boolean checkProfileExists() {
		
		ArrayList<ProfileDTO> profiles = Profile.getProfile(this);
		
		if (profiles.size() > 0) {
			return true;
		} else {
			return false;
		}
		
	} // end checkProfileExists()
	
} // end class ProfileActivity